package cn.wj.school.cloud;

import cn.wj.school.cloud.freemarker.JsonStringEscape;
import cn.wj.school.cloud.freemarker.SimpleFreeMarkerConfigurer;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import com.google.common.collect.Maps;
import freemarker.template.utility.HtmlEscape;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Map;
import java.util.Properties;

/**
 * @Author: yueding
 * @Description:
 * @Date: Created in 19:47 2019/1/3
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
@EnableDubboConfiguration
@ServletComponentScan
@SpringBootApplication
public class JteachApplication {

    public static void main(String[] args) {
        SpringApplication.run(JteachApplication.class, args);
    }

    @Bean
    @Primary
    public FreeMarkerConfigurer freemarkerConfig(FreeMarkerProperties properties) {
        SimpleFreeMarkerConfigurer factory = new SimpleFreeMarkerConfigurer();
        factory.setTemplateLoaderPaths(properties.getTemplateLoaderPath());
        factory.setPreferFileSystemAccess(properties.isPreferFileSystemAccess());
        factory.setDefaultEncoding(properties.getCharsetName());
        Properties settings = new Properties();
        settings.putAll(properties.getSettings());
        factory.setFreemarkerSettings(settings);
        Map<String, Object> variables = Maps.newHashMap();
        variables.put("html_escape", new HtmlEscape());
        variables.put("json_escape", new JsonStringEscape());
        factory.setFreemarkerVariables(variables);
        return factory;
    }
}
